<template>
  <div class="loan-term-selector">
    <div class="term-options">
      <button 
        v-for="term in terms" 
        :key="term"
        class="term-btn"
        :class="{ active: modelValue === term }"
        @click="selectTerm(term)"
      >
        <span class="term-value">{{ term }}</span>
        <span class="term-unit">个月</span>
      </button>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

const props = defineProps({
  modelValue: {
    type: Number,
    required: true
  },
  terms: {
    type: Array,
    default: () => [3, 6, 12, 24, 36]
  }
});

const emit = defineEmits(['update:modelValue', 'change']);

// 选择期限
const selectTerm = (term) => {
  emit('update:modelValue', term);
  emit('change', term);
};
</script>

<style scoped lang="scss">
.loan-term-selector {
  width: 100%;
  
  .term-options {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    
    .term-btn {
      flex: 1;
      min-width: 60px;
      height: 44px;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      background-color: #fff;
      color: #606266;
      font-size: 14px;
      cursor: pointer;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: all 0.3s;
      
      &:hover {
        border-color: #c0c4cc;
      }
      
      &.active {
        background-color: #3a4a6d;
        color: #fff;
        border-color: #3a4a6d;
      }
      
      .term-value {
        font-size: 16px;
        font-weight: bold;
      }
      
      .term-unit {
        font-size: 12px;
        margin-top: 2px;
      }
    }
  }
}
</style>